package com.alks.function.controller.finance.mtlcosts;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.WebLogger;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.finance.fincons.InboundDetailsDto;
import com.alks.function.data.request.finance.mtlcosts.MtlPreOtherFindRequest;
import com.alks.function.data.request.finance.mtlcosts.MtlPreOtherUpdateRequest;
import com.alks.function.data.request.finance.ryfinance.MtlPreOtherInRequest;
import com.alks.function.service.finance.mtlcosts.IMcRecTbAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author MS
 * @since 2024/6/3上午11:37
 */

@RestController
@ApiRestController("/mtlCosts")
@Api(value = "财务部", tags = "财务部-材料费用-收货对账作业-1")
@Slf4j
public class McRecTbAccountController {
    @Autowired
    private IMcRecTbAccountService service;

    @WebLogger
    @PostMapping("/mtlPreOtherInQuery")
    @ApiOperation(value = "入库明细查询", tags = "财务部-材料费用-收货对账作业-1")
    public ResponseInfo mtlPreOtherInQuery(@RequestBody MtlPreOtherInRequest request) {
        Map<String, Object> map = service.mtlPreOtherInQuery(request);
        return ResponseInfo.ok(map);
    }

    @WebLogger
    @PutMapping("/mtlPreOtherInUp")
    @ApiOperation(value = "入库明细更新", tags = "财务部-材料费用-收货对账作业-1")
    public ResponseInfo mtlPreOtherInUp(@RequestBody List<MtlPreOtherUpdateRequest> request,
                                        @RequestParam(required = false) @ApiParam("备注信息") String remark,
                                        @RequestParam(required = false) @ApiParam("结算厂商") String vendorName2) {
        return service.mtlPreOtherInUp(request, remark, vendorName2);
    }

    @WebLogger
    @PutMapping("/mtlPreOtherInUp2")
    @ApiOperation(value = "结算厂商置空", tags = "财务部-材料费用-收货对账作业-1")
    public ResponseInfo mtlPreOtherInUp2(@RequestBody List<MtlPreOtherUpdateRequest> request) {
        return service.mtlPreOtherInUp2(request);
    }

    @WebLogger
    @PutMapping("/mtlPre/up/remark")
    @ApiOperation(value = "备注更新", tags = "财务部-材料费用-收货对账作业-1")
    public ResponseInfo mtlPreUpRemark(@RequestParam(required = false) @ApiParam("备注信息") String remark,
                                       @RequestParam(required = false) @ApiParam("类型") String seqName,
                                       @RequestParam(required = false) @ApiParam("主键") Integer id) {
        return service.mtlPreUpRemark(seqName, id, remark);
    }

    @WebLogger
    @PutMapping("/mtlPreOtherInFinance")
    @ApiOperation(value = "对账作业", tags = "财务部-材料费用-收货对账作业-1")
    public ResponseInfo mtlPreOtherInFinance(@RequestBody List<MtlPreOtherFindRequest> request,
                                             @RequestParam(required = false) @ApiParam("对账月份") String financeYymm,
                                             @RequestParam(required = false) @ApiParam("对账方式") String accountName,
                                             @RequestParam @ApiParam(required = true, value = "0.取消对账 1.对账") Integer flag) {
        return service.mtlPreOtherInFinance(request, financeYymm, accountName, flag);
    }

    @WebLogger
    @GetMapping("/mtlPreOtherInBack")
    @ApiOperation(value = "备注信息回显查询", tags = "财务部-材料费用-收货对账作业-1")
    public ResponseInfo mtlPreOtherInBack() {
        return service.mtlPreOtherInBack();
    }

    @WebLogger
    @PutMapping("/mtlPreOther/export")
    @ApiOperation(value = "导出", tags = "财务部-材料费用-收货对账作业-1")
    public void export(@RequestBody MtlPreOtherInRequest request, HttpServletResponse response) {
        request.setPageSize(-1);
        String templet = "收货对账作业_1模版.xlsx";
        String fileName = "收货对账作业_1" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyMMdd")) + ".xlsx";
        Map<String, Object> map = service.mtlPreOtherInQuery(request);
        List<InboundDetailsDto> dtoList = (List<InboundDetailsDto>) map.get("data");
        BigDecimal amtSum = (BigDecimal) map.get("amtSum");
        InboundDetailsDto dto = new InboundDetailsDto();
        dto.setAmt(amtSum);
        dto.setSeqName("合计");
        dtoList.add(dto);
        try {
            ExcelUtils.creatByTemplate(response, templet, fileName, dtoList);
        } catch (Exception e) {
            throw new ServiceErrorException("导出失败");
        }
    }
}